home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextLibrary / Documentation / NextDev / Examples / DriverKit / DriverInspector / IOBeepInspector.m < prev    next >
Encoding:
Text File  |  1995-02-01  |  6.9 KB  |  252 lines

  1. // IOBeepInspector.m
  2. // A system beep inspector
  3. // Copyright 1993 by NeXT, Incorporated.  All Rights Reserved.
  4.  
  5. // This is not a subclass of IODeviceInspector, since it does not need
  6. // to access any of the standard controls.  Therefore, we must implement
  7. // our own loading of the nib, and otherwise conform to the
  8. // IOConfigurationInspector protocol.
  9.  
  10. #import "IOBeepInspector.h"
  11.  
  12. #import <driverkit/IODeviceMaster.h>
  13. #import <appkit/Control.h>
  14. #import <stdlib.h>
  15.  
  16. #define DRIVER_NAME "Beep"
  17. #define PITCH_KEY "Frequency"
  18. #define DURATION_KEY "Duration"
  19. #define STYLE_KEY "Style"
  20.  
  21. @implementation IOBeepInspector
  22.  
  23. // We're a subclass of Object, so this is the only init necessary
  24. - init
  25. {
  26.     char  buff[MAXPATHLEN];
  27.     IOReturn ret;
  28.     IOString kind;
  29.     
  30.     if (![[NXBundle bundleForClass: [self class]] getPath: buff
  31.         forResource:"Inspector" ofType:"nib"])
  32.     {
  33.     NXLogError("Couldn't load inspector nib");
  34.     [self free];
  35.     return nil;
  36.     }
  37.     if (![NXApp loadNibFile:buff owner:self withNames:NO]) {
  38.     [self free];
  39.     return nil;
  40.     }
  41.     // Note the dichotomy here...
  42.     stylePopup = [styleButton target];
  43.     [stylePopup setAction:@selector(styleDidChange:)];
  44.     [stylePopup setTarget: self];
  45.     
  46.     // Set up the IODeviceMaster object to talk to the driver
  47.     devMaster = [IODeviceMaster new];
  48.     ret = [devMaster lookUpByDeviceName: DRIVER_NAME objectNumber:&myTag
  49.     deviceKind:&kind];
  50.     if (ret != IO_R_SUCCESS) { // We might not be loaded, so just log the error
  51.     NXLogError("Couldn't find Beep device: error %d\n", ret);
  52.     }
  53.     
  54.     // Be reasonable
  55.     maxFreq = [frequencySlider maxValue];
  56.     minFreq = [frequencySlider minValue];
  57.     maxDuration = [durationSlider maxValue];
  58.     minDuration = [durationSlider minValue];
  59.     
  60.     return self;
  61. }
  62.  
  63. - free
  64. {
  65.     [devMaster free];
  66.     return [super free];
  67. }
  68.  
  69. // This gets called by the Configure app after we've loaded our nib
  70. // but before it gets displayed.  We try to get values from the driver first,
  71. // then from the table.  If those fail, we use the values in the nib.
  72. - setTable: (NXStringTable *)instance
  73. {
  74.     unsigned int pitch, duration;
  75.     const char *tempStr;
  76.     
  77.     table = instance;
  78.     if([self getIntValue: &pitch forParameter: PITCH_KEY] == IO_R_SUCCESS) {
  79.         [frequencySlider setIntValue: pitch];
  80.     [frequencyField setStringValue: [frequencySlider stringValue]];
  81.     } else {
  82.     tempStr = [table valueForStringKey: PITCH_KEY];
  83.     strcpy(pitchString, tempStr ? tempStr : "");
  84.     if(strlen(pitchString)) {
  85.         [frequencySlider setIntValue: atoi(pitchString)];
  86.         [frequencyField setStringValue: pitchString];
  87.     } else {
  88.         [self frequencyDidChange: frequencySlider];
  89.     }
  90.     }
  91.     if([self getIntValue: &duration forParameter: DURATION_KEY] == IO_R_SUCCESS) {
  92.         [durationSlider setIntValue: duration];
  93.     [durationField setStringValue: [durationSlider stringValue]];
  94.     } else {
  95.     tempStr = [table valueForStringKey: DURATION_KEY];
  96.     strcpy(durationString, tempStr ? tempStr : "");
  97.     if(strlen(durationString)) {
  98.         [durationSlider setIntValue: atoi(durationString)];
  99.         [durationField setStringValue: durationString];
  100.     } else {
  101.         [self durationDidChange: durationSlider];
  102.     }
  103.     }
  104.     // A little redundancy for clarity
  105.     if([self getStringValue: styleString forParameter: STYLE_KEY] == IO_R_SUCCESS) {
  106.     [[stylePopup itemList] selectCellWithTag:
  107.         [stylePopup indexOfItem: styleString]];
  108.     [styleButton setTitle: styleString];
  109.     } else {
  110.     tempStr = [table valueForStringKey: STYLE_KEY];
  111.     strcpy(styleString, tempStr ? tempStr : "");
  112.     if(strlen(styleString)) {
  113.         // Remember to set the tags for the MenuCells properly in IB
  114.         [[stylePopup itemList] selectCellWithTag:
  115.         [stylePopup indexOfItem: styleString]];
  116.         [styleButton setTitle: styleString];
  117.     } else {
  118.         [[stylePopup itemList] selectCellWithTag: 0];
  119.         [self styleDidChange: stylePopup];
  120.     }
  121.     }
  122.     return self;
  123. }
  124.  
  125. // This returns the connection to our "invisible" box in the nib
  126. - (View *)inspectionView
  127. {
  128.     // Some may think this is annoying...
  129.     NXBeep();
  130.     return beepView;
  131. }
  132.  
  133. // We don't use any of these...
  134. - resourcesChanged: (IOResources *)rsrc
  135. {
  136.     return self;
  137. }
  138.  
  139. // We update the table each time one of these methods gets called.
  140. // Make sure the slider isn't continuous in IB!
  141. - frequencyDidChange: sender
  142. {
  143.     int val = [sender intValue];
  144.  
  145.     if(val < minFreq) {
  146.         val = minFreq;
  147.     [sender setIntValue: val];
  148.     } else
  149.     if(val > maxFreq) {
  150.     val = maxFreq;
  151.     [sender setIntValue: val];
  152.     }
  153.     sprintf(pitchString, "%d", val);
  154.     [table insertKey: PITCH_KEY value: (void *)pitchString];
  155.     if (sender == frequencySlider) {
  156.         [frequencyField setIntValue: val];
  157.     } else {
  158.     [frequencySlider setIntValue: val];
  159.     }
  160.     [self setIntValue: &val forParameter: PITCH_KEY];
  161.     NXBeep();
  162.     return self;
  163. }
  164.  
  165. - durationDidChange: sender
  166. {
  167.     int val = [sender intValue];
  168.     
  169.     // Sanity check
  170.     if(val < minDuration) {
  171.         val = minDuration;
  172.     [sender setIntValue: val];
  173.     }
  174.     else
  175.     if(val > maxDuration) {
  176.     val = maxDuration;
  177.     [sender setIntValue: val];
  178.     }
  179.     sprintf(durationString, "%d", val);
  180.     [table insertKey: DURATION_KEY value: (void *)durationString];
  181.     if (sender == durationSlider) {
  182.         [durationField setIntValue: val];
  183.     } else {
  184.     [durationSlider setIntValue: val];
  185.     }
  186.     [self setIntValue: &val forParameter: DURATION_KEY];
  187.     NXBeep();
  188.     return self;
  189. }
  190.  
  191. - styleDidChange: sender
  192. {
  193.     strcpy(styleString, [stylePopup selectedItem]);
  194.     [table insertKey: STYLE_KEY value: (void *)styleString];
  195.     [self setStringValue: styleString forParameter: STYLE_KEY];
  196.     NXBeep();
  197.     return self;
  198. }
  199.  
  200. - (IOReturn)getIntValue: (unsigned *)value forParameter: (IOParameterName)param
  201. {
  202.     IOReturn ret;
  203.     static unsigned int one = 1;
  204.     
  205.     ret = [devMaster getIntValues: value forParameter: param objectNumber: myTag
  206.     count: &one];
  207.     if (ret != IO_R_SUCCESS) {
  208.     NXLogError("Couldn't get value for %s", param);
  209.     return ret;
  210.     }
  211.     return IO_R_SUCCESS;
  212. }
  213.  
  214. - (IOReturn)getStringValue: (IOCharParameter)value forParameter: (IOParameterName)param
  215. {
  216.     IOReturn ret;
  217.     static unsigned int len = IO_MAX_PARAMETER_ARRAY_LENGTH;
  218.     
  219.     ret = [devMaster getCharValues: value forParameter: param objectNumber: myTag
  220.     count: &len];
  221.     if (ret != IO_R_SUCCESS) {
  222.     NXLogError("Couldn't get value for %s", param);
  223.     return ret;
  224.     }
  225.     return IO_R_SUCCESS;
  226. }
  227.  
  228. - setIntValue: (unsigned *)value forParameter: (IOParameterName)param
  229. {
  230.     IOReturn ret;
  231.     
  232.     ret = [devMaster setIntValues: value forParameter: param objectNumber: myTag
  233.     count: 1];
  234.     if (ret != IO_R_SUCCESS) {
  235.     NXLogError("Couldn't set value for %s", param);
  236.     }
  237.     return self;
  238. }
  239.  
  240. - setStringValue: (IOCharParameter)value forParameter: (IOParameterName)param
  241. {
  242.     IOReturn ret;
  243.     
  244.     ret = [devMaster setCharValues: value forParameter: param
  245.         objectNumber: myTag count: strlen(value) + 1];
  246.     if (ret != IO_R_SUCCESS) {
  247.     NXLogError("Couldn't set value for %s", param);
  248.     }
  249.     return self;
  250. }
  251.  
  252. @end